package com.project2.service;

import com.github.pagehelper.PageHelper;


import com.github.pagehelper.PageInfo;
import com.project2.bean.CommonData;
import com.project2.bean.PageBo;
import com.project2.bean.bo.AdminLoginBo;

import com.project2.bean.bo.UserUpdateBo;
import com.project2.bean.po.*;

import com.project2.bean.vo.*;
import com.project2.mapper.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

/**
 * TODO
 *
 * @param
 * @author wj
 * @return
 */
@Component
public class AdminAuthServiceImpl implements AdminAuthService {
    @Autowired
    MarketAdminMapper marketAdminMapper;
    @Autowired
    MarketUserMapper marketUserMapper;
    @Autowired
    MarketAddressMapper marketAddressMapper;
    @Autowired
    MarketCollectMapper marketCollectMapper;
    @Autowired
    MarketFootprintMapper marketFootprintMapper;
    @Autowired
    MarketSearchHistoryMapper marketSearchHistoryMapper;
    @Autowired
    MarketFeedbackMapper marketFeedbackMapper;
    @Autowired
    MarketNoticeMapper marketNoticeMapper;
    @Autowired
    MarketNoticeAdminMapper marketNoticeAdminMapper;
    @Autowired
    MarketRoleMapper marketRoleMapper;

    @Override
    public AuthLoginVo login(AdminLoginBo adminLoginBo) {
        MarketAdminExample example = new MarketAdminExample();
        MarketAdminExample.Criteria criteria = example.createCriteria();
        if (adminLoginBo.getUsername()!=null){
            criteria.andUsernameEqualTo(adminLoginBo.getUsername());
        }
       if (adminLoginBo.getPassword()!=null){
           criteria.andPasswordEqualTo(adminLoginBo.getPassword());
       }
        List<MarketAdmin> marketAdmins = marketAdminMapper.selectByExample(example);
        if (marketAdmins.size()==0){
            return null;
        }
        AuthLoginVo loginUserData = new AuthLoginVo();
        AuthLoginInfo adminInfo = new AuthLoginInfo();
        for (MarketAdmin marketUser : marketAdmins) {
            adminInfo.setAvatar(marketUser.getAvatar());
            adminInfo.setNickName(marketUser.getUsername());
            // String roleIds = marketUser.getRoleIds();
            // Integer roleId = Integer.valueOf(roleIds.substring(1, roleIds.length() - 1));
            // if (roleId ==1){
            //     loginUserData.setToken(TokenProccessor.getInstance().makeToken());
            // }else if (roleId==2){
            //     loginUserData.setToken(TokenProccessor.getInstance().makeToken());
            // }else if (roleId==3){
            //     loginUserData.setToken(TokenProccessor.getInstance().makeToken());
            // }
        }
        loginUserData.setAdminInfo(adminInfo);
        loginUserData.setToken("c4d988f3-e4c5-4e46-b6ce-13b9e008ea20");

        return loginUserData;
    }

    @Override
    public DashBoardVo dashboard() {
        int i=marketUserMapper.selectCountGoods();
        int j=marketUserMapper.selectCountOrder();
        int k=marketUserMapper.selectCountProduct();
        int n=marketUserMapper.selectCountUser();

        DashBoardVo dashBoardVo = new DashBoardVo();
        dashBoardVo.setGoodsTotal(i);
        dashBoardVo.setOrderTotal(j);
        dashBoardVo.setProductTotal(k);
        dashBoardVo.setUserTotal(n);
        return dashBoardVo;
    }

    @Override
    public UserListVo list(PageBo pageBo, String username, String mobile) {
        MarketUserExample marketUserExample = new MarketUserExample();

        MarketUserExample.Criteria criteria = marketUserExample.createCriteria();
        criteria.andDeletedEqualTo(false);
        if (username!=null ){
            criteria.andUsernameLike("%"+username+"%");
        }if (mobile!=null){
            criteria.andMobileEqualTo(mobile);
        }
        marketUserExample.setOrderByClause(pageBo.getSort()+" "+pageBo.getOrder());
        PageHelper.startPage(pageBo.getPage(),pageBo.getLimit());

        List<MarketUser> list=marketUserMapper.selectByExample(marketUserExample);

        PageInfo<MarketUser> userPoPageInfo = new PageInfo<>(list);
        UserListVo userListVo = new UserListVo();
        userListVo.setLimit(pageBo.getLimit());
        userListVo.setPage(userPoPageInfo.getPageNum());
        userListVo.setList(list);
        userListVo.setPages(userPoPageInfo.getPages());
        userListVo.setTotal(userPoPageInfo.getSize());
        return userListVo;
    }

    @Override
    public AddressListVo list(PageBo pageBo, String username, Integer userId) {

        MarketAddressExample example = new MarketAddressExample();
        MarketAddressExample.Criteria criteria = example.createCriteria();
        criteria.andDeletedEqualTo(false);
        if (username!=null ){
            criteria.andNameLike("%"+username+"%");;
        }
        if (userId!=null){
            criteria.andUserIdEqualTo(userId);
        }
        example.setOrderByClause(pageBo.getSort()+" "+pageBo.getOrder());
        PageHelper.startPage(pageBo.getPage(),pageBo.getLimit());

        List<MarketAddress> list=marketAddressMapper.selectByExample(example);

        PageInfo<MarketAddress> userPoPageInfo = new PageInfo<>(list);

        AddressListVo addressListVo = new AddressListVo();

        addressListVo.setLimit(pageBo.getLimit());
        addressListVo.setPage(userPoPageInfo.getPageNum());
        addressListVo.setList(list);
        addressListVo.setPages(userPoPageInfo.getPages());
        addressListVo.setTotal(userPoPageInfo.getSize());
        return addressListVo;
    }

    @Override
    public CollectListVo list(PageBo pageBo, Integer valueId, Integer userId) {
        MarketCollectExample example = new MarketCollectExample();

        MarketCollectExample.Criteria criteria = example.createCriteria();
        criteria.andDeletedEqualTo(false);
        if (valueId!=null ){
            criteria.andValueIdEqualTo(valueId);
        }
        if (userId!=null){
            criteria.andUserIdEqualTo(userId);
        }
        example.setOrderByClause(pageBo.getSort()+" "+pageBo.getOrder());
        PageHelper.startPage(pageBo.getPage(),pageBo.getLimit());
        List<MarketCollect> list=marketCollectMapper.selectByExample(example);
        PageInfo<MarketCollect> userPoPageInfo = new PageInfo<>(list);
        CollectListVo collectListVo = new CollectListVo();
        collectListVo.setLimit(pageBo.getLimit());
        collectListVo.setPage(userPoPageInfo.getPageNum());
        collectListVo.setList(list);
        collectListVo.setPages(userPoPageInfo.getPages());
        collectListVo.setTotal(userPoPageInfo.getSize());
        return collectListVo;


    }

    @Override
    public int updateUser(UserUpdateBo userUpdateBo) {
        int i = marketUserMapper.updateUser(userUpdateBo);
        return i;
    }

    @Override
    public MarketUser detail(Integer id) {
        MarketUser marketUser = marketUserMapper.selectByPrimaryKey(id);
        return marketUser;
    }

    @Override
    public FootprintListVo footList(PageBo pageBo, Integer goodsId, Integer userId) {
        MarketFootprintExample example = new MarketFootprintExample();

        MarketFootprintExample.Criteria criteria = example.createCriteria();
        criteria.andDeletedEqualTo(false);
        if (goodsId!=null ){
            criteria.andGoodsIdEqualTo(goodsId);
        }
        if (userId!=null){
            criteria.andUserIdEqualTo(userId);
        }
        example.setOrderByClause(pageBo.getSort()+" "+pageBo.getOrder());
        PageHelper.startPage(pageBo.getPage(),pageBo.getLimit());
        List<MarketFootprint> list=marketFootprintMapper.selectByExample(example);
        PageInfo<MarketFootprint> userPoPageInfo = new PageInfo<>(list);
        FootprintListVo footprintListVo = new FootprintListVo();
        footprintListVo.setLimit(pageBo.getLimit());
        footprintListVo.setPage(userPoPageInfo.getPageNum());
        footprintListVo.setList(list);
        footprintListVo.setPages(userPoPageInfo.getPages());
        footprintListVo.setTotal(userPoPageInfo.getSize());
        return footprintListVo;
    }

    @Override
    public HistoryListVo historyList(PageBo pageBo, String keyword, Integer userId) {
        MarketSearchHistoryExample example = new MarketSearchHistoryExample();

        MarketSearchHistoryExample.Criteria criteria = example.createCriteria();
        criteria.andDeletedEqualTo(false);
        if (keyword!=null ){
            criteria.andKeywordLike("%"+keyword+"%");;
        }
        if (userId!=null){
            criteria.andUserIdEqualTo(userId);
        }
        example.setOrderByClause(pageBo.getSort()+" "+pageBo.getOrder());

        PageHelper.startPage(pageBo.getPage(),pageBo.getLimit());
        List<MarketSearchHistory> list=marketSearchHistoryMapper.selectByExample(example);
        PageInfo<MarketSearchHistory> userPoPageInfo = new PageInfo<>(list);
        HistoryListVo historyListVo = new HistoryListVo();
        historyListVo.setLimit(pageBo.getLimit());
        historyListVo.setPage(userPoPageInfo.getPageNum());
        historyListVo.setList(list);
        historyListVo.setPages(userPoPageInfo.getPages());
        historyListVo.setTotal(userPoPageInfo.getSize());
        return historyListVo;
    }

    @Override
    public FeedbackListVo feedbackList(PageBo pageBo, String username, Integer id) {
        MarketFeedbackExample example = new MarketFeedbackExample();

        MarketFeedbackExample.Criteria criteria = example.createCriteria();
        criteria.andDeletedEqualTo(false);
        if (username!=null ){
            criteria.andUsernameLike("%"+username+"%");;
        }
        if (id!=null){
            criteria.andIdEqualTo(id);
        }
        example.setOrderByClause(pageBo.getSort()+" "+pageBo.getOrder());
        PageHelper.startPage(pageBo.getPage(),pageBo.getLimit());
        List<MarketFeedback> list=marketFeedbackMapper.selectByExample(example);
        PageInfo<MarketFeedback> userPoPageInfo = new PageInfo<>(list);
        FeedbackListVo feedbackListVo = new FeedbackListVo();
        feedbackListVo.setLimit(pageBo.getLimit());
        feedbackListVo.setPage(userPoPageInfo.getPageNum());
        feedbackListVo.setList(list);
        feedbackListVo.setPages(userPoPageInfo.getPages());
        feedbackListVo.setTotal(userPoPageInfo.getSize());
        return feedbackListVo;
    }

    @Override
    public AdminInfoVo info(MarketAdmin attribute) {
        AdminInfoVo infoData = new AdminInfoVo();
        infoData.setName(attribute.getUsername());
        //根据primaryPrincipal做查询
        infoData.setAvatar(attribute.getAvatar());

        Integer[] roleIds = attribute.getRoleIds();
        ArrayList<String> roles = new ArrayList<>();
        ArrayList<String> perms = new ArrayList<>();
        for (Integer roleId : roleIds) {
            // if (roleId==1){
            //     roles.add("超级管理员");
            // }else if (roleId==2){
            //     roles.add("商城管理员");
            // }else if (roleId==3){
            //     roles.add("推广管理员");
            // }else {
            //     roles.add(roleId.toString());
            // }
            MarketRole marketRole = marketRoleMapper.selectByPrimaryKey(roleId);
            roles.add(marketRole.getName());
            List<String> list2 = marketRoleMapper.queryPermissionByRoleId(roleId.toString());
            for (String s : list2) {
                perms.add(s);
            }
        }
        infoData.setRoles(roles);
        infoData.setPerms(perms);
        return infoData;
    }
}
